<template>
  <base-breadcrumb>
    <searchData :search-data="searchArr" @search="search" />
    <a-card :bordered="false">
      <list-table
        ref="table"
        row-key="id"
        :columns="columns"
        :request="loadData"
        :export-config="{ use: true,select: '分包合同列表',all: '分包合同列表'}"
        :export-permission="{ select: 'lcm_008_exp_04',all: 'lcm_008_exp_04'}"
        :statisticsConfig="{use: true,statistics,temp}"
      >
        <template slot="prefix">
          <a-button
            v-btn-permission="'lcm_008_add_02'"
            type="primary"
            icon="plus"
            @click="handleAdd('普通')"
          >
            新增分包合同
          </a-button>
          <a-button
            v-btn-permission="'lcm_008_add_03'"
            type="primary"
            icon="plus"
            @click="handleAdd('协议')"
          >
            新增补充协议
          </a-button>
        </template>
        <template slot="suffix">
          <span style="float:right;">云筑数据更新时间：{{ ynzhuDate }}</span>
        </template>
        <span slot="index" slot-scope="text, record, index">{{ index + 1 }}</span>
        <template slot="auditStatusStr" slot-scope="text, record">
          <list-status :status="record.auditStatus" :text="text" conversion="conversion" :is-approval="true" />
        </template>
        <template slot="action" slot-scope="text, record">
          <MoreButton>
            <a v-btn-permission="'lcm_008_loo_01'" @click="getDetails(record)">查看</a>
            <template
              v-if="((record.auditStatus === 'DRAFT' || record.auditStatus === 'REJECTED') && record.creatorId === $userInfo().id) && hideBuquBtn()"
            >
              <a @click="handleEdit(record)">编辑</a>
              <a @click="handleDel(record.id)">删除</a>
            </template>
            <template v-if="record.fromStatusStr && record.fromStatusStr === '云筑'">
              <a v-if="record.auditStatus==='AMEND_RECORD' || record.auditStatus==='REJECTED'" v-btn-permission="'lcm_005_bul_09'" @click="handleEdit(record, true)">补录</a>
            </template>
            <approval-button :id="record.id" :status="record.auditStatus"
                             v-if="record.auditStatus === 'PAST' || record.auditStatus === 'SUBMITTED' || record.auditStatus === 'REJECTED' || record.auditStatus === 'REVOKEIN' || record.auditStatus === 'REVOKE' || record.auditStatus === 'ABOLISH' || record.auditStatus === '1' || record.auditStatus === '3' || record.auditStatus === 'COMMIT'|| record.auditStatus === '2' " />
          </MoreButton>
        </template>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>

<script>
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 分公司
import OrgTreeSelect from '@/components/OrgTreeSelect'
import subcontract from '@/api/legal/branchContract/subcontract'
import BSelect from '@/components/BComponents/BSelect'


export default {
  name: 'Contractors',
  components: {},
  data() {
    return {
      queryParam: {},
      visible: false,
      loading: false,
      ynzhuDate: '',
      pageNo: 1,
      pageSize: 100,
      totalPage: 0,
      subcontractorData:[],
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, { query: this.queryParam })
        subcontract.findCount(requestParameters).then((res)=>{
           this.statistics = res.data
        })
        subcontract.getYnzhuDate().then(res => this.ynzhuDate = res.message)
        return subcontract.list(requestParameters)
      },
      columns: [
        {
          title: '序号',
          width: 70,
          scopedSlots: { customRender: 'index' }
        },
        {
          title: '所属分公司',
          dataIndex: 'branchComName'
        },
        {
          title: '项目名称',
          dataIndex: 'projectName'
        },
        {
          title: '合同名称',
          dataIndex: 'contractName'
        },
        {
          title: '合同编号',
          dataIndex: 'code'
        },
        {
          title: '分包商',
          dataIndex: 'subSupplyCompany'
        },
        {
          title: '合同种类',
          dataIndex: 'localContractTypeStr'
        },
        {
          title: '分包方式',
          dataIndex: 'contractTypeStr'
        },
        {
          title: '合同额（含税)(元）',
          dataIndex: 'contractInTaxAmount'
        },
        {
          title: '合同来源',
          dataIndex: 'fromStatusStr'
        },
        {
          title: '状态',
          dataIndex: 'auditStatusStr',
          scopedSlots: { customRender: 'auditStatusStr' }
        },
        {
          title: '操作',
          width: '240px',
          fixed: 'right',
          scopedSlots: { customRender: 'action' }
        }
      ],
      contractTypeData: [],
      statistics: {},
      temp: () => {
        return (
          <span>
              <span>| 主合同额：<a>{this.statistics.mainAmount || 0}元</a> | </span>
              <span>补充协议合同额：<a>{this.statistics.bcAmount || 0}元</a> | </span>
              <span>总合同额：<a>{this.statistics.allAmount || 0}元</a></span>
            </span>
        )
      }
    }
  },
  computed: {
    SelectSubCompanyAutoSelected() {
      return this.$currentPost().code !== 'CO'
    },
    SelectProjectAutoSelected() {
      return this.$currentPost().code === 'PD'
    },
    searchArr(){
       return [
        // 搜索框
        {
          name: '所属单位',
          valueKey: 'searchQuery',
          type: OrgTreeSelect,
        },
        // {
        //   name: '所属分公司',
        //   type: SelectSubCompany,
        //   valueKey: 'branchComId',
        //   autoSelected: this.SelectSubCompanyAutoSelected
        // },
        // {
        //   name: '项目名称',
        //   type: SelectProject,
        //   autoSelected: this.SelectProjectAutoSelected,
        //   valueKey: 'projectId',
        //   keyValue: 'branchComId'
        // },
        {
          name: '分包商',
          type: 'input',
          value: 'subSupplyCompany'
        },
        //  {
        //        type: BSelect,valueKey: 'subcontractorScc', name: '分包商',
        //        list: this.subcontractorData,
        //       on: {
        //         change: (e) => {
        //           let findName = this.subcontractorData.find((item)=>item.id === e);
        //           this.form.subcontractorName = findName?.name;
        //         },
        //         search:(e)=>{
        //           this.pageNo = 1;
        //           this.getSubContractorAllFn(e);

        //         },
        //         popupScroll:(e)=>{
        //          const { target } = e
        //         var total = target.scrollTop + target.offsetHeight;
        //         var scrollHeight = target.scrollHeight

        //         // this.pageNo是当前页 this.totalPage是总页数
        //         if (total >= scrollHeight - 1 && total < scrollHeight + 1) {
        //           this.pageNo = this.pageNo + 1
        //           this.getSubContractorAllFn();
        //         }
        //        }
        //     }
        //     },

        {
          name: '合同名称',
          type: 'input',
          value: 'contractName'
        },
        {
          name: '合同编号',
          type: 'input',
          value: 'code'
        },
        {
          name: '合同种类',
          type: BSelect,
          valueKey: 'localContractType',
          list: [
            { id: 0, name: '主合同' },
            { id: 1, name: '补充协议' }
          ]
        },
        {
          name: '分包方式',
          type: 'select',
          value: 'contractTypeId',
          data: this.contractTypeData,
          // request: async () => {
        
          // }
        },
        {
          name: '合同来源',
          type: BSelect,
          valueKey: 'fromStatus',
          data: [],
          list: [
            { id: 0, name: '云筑' },
            { id: 1, name: '本平台新增' }
          ]
        }
      ]
    }
  },
 async mounted(){
     //await this.getSubContractorAllFn();
     this.contractTypeData = [];
      let res = await subcontract.typesLabel({ query: { type: 21 } })
        if (res.code === this.SUCCESS_CODE) {
          res.data?.map((v)=>{
             this.contractTypeData.push({ label: v.name, value: v.id })
          })
        }
},
  methods: {
    async getSubContractorAllFn(e){
         let req = {
           pageNo:this.pageNo,
           pageSize:this.pageSize,
           query:{
             name:e
           }
         }
      let {data} = await subcontract.getSubContractorAll(req);
      this.subcontractorData = data;
    },
    handleDel(id) {
      let params = id ? [id] : this.selectId
      this.$delList2(subcontract.del, () => {
        this.search()
      }, { ids: params })
    },
    search(val) {
      val && (this.queryParam = val)
      this.$refs.table.getData(null, true)
    },
    handleAdd(type) {
      switch (type) {
        case '普通':
          this.$router.push({ path: '/legal/branch-contract/editOrdinaryContract', query: { isContract: 1,detail: 2} })
          break
        case '协议':
          this.$router.push({ path: '/legal/branch-contract/editSupplementaryagreement', query: { detail: 2} })
          break
      }
    },
    getDetails(record) {
      if (record.fromStatusStr === '云筑') {
        return this.$router.push({
          path: '/legal/branch-contract/editCloudBuildInput',
          query: { id: record.id, detail: 1 }
        })
      }
      // return this.$router.push({ path: '/legal/branch-contract/editCloudBuildInput', query: { id: record.id } })
      if (record.processTrend === 3 || record.processTrend === '3') {
        this.$router.push({
          path: '/legal/branch-contract/editSupplementaryagreement',
          query: { id: record.id, detail: 1 }
        })
      } else {
        this.$router.push({
          path: '/legal/branch-contract/editOrdinaryContract',
          query: { id: record.id, detail: 1 }
        })
      }
    },
    handleEdit(record, flag) {
      if (flag) {
        return this.$router.push({
          path: '/legal/branch-contract/editCloudBuildInput',
          query: { id: record.id,detail: 2}
        })
      }
      if (record.processTrend === 3 || record.processTrend === '3') {
        this.$router.push({
          path: '/legal/branch-contract/editSupplementaryagreement',
          query: { id: record.id,detail: 2}
        })
      } else {
        this.$router.push({
          path: '/legal/branch-contract/editOrdinaryContract',
          query: { id: record.id,detail: 2}
        })
      }
    }
  }
}
</script>
